package common.store.web.controller;

import common.store.admin.service.CategoryService;
import common.store.admin.service.impl.CategoryServiceImpl;
import common.store.web.service.ProductService;
import common.store.web.service.impl.ProductServiceImpl;
import common.store.web.vo.PageVo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/productList")
public class ProductListServlet extends HttpServlet{


    private ProductService productService;

    private CategoryService categoryService;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        //从前端获取的
        String currentPage = req.getParameter("currentPage") == null ? "1" : req.getParameter("currentPage");

        //从前端接收分类主键 cid
        String cid = req.getParameter("cid");

        Integer maxCount = 12;//也可以由前端传递

        productService = new ProductServiceImpl();
        categoryService = new CategoryServiceImpl();

        //List<Product> productList = productService.getProductList();
        PageVo pageVo = productService.getPageVoByCurrentPageAndMaxCount(Integer.valueOf(currentPage) , maxCount , cid);
        req.setAttribute("pageVo" , pageVo);
        req.setAttribute("categoryList" , categoryService.getCategoryList());
        req.getRequestDispatcher("/product_list.jsp").forward(req , resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req ,resp);
    }
}
